<?php


abstract class BaseKernelSucursal extends BaseObject  implements Persistent {


	
	protected static $peer;

	
	protected $id;

	
	protected $nombre;

	
	protected $resolucion;

	
	protected $ubicacionid;

	
	protected $factura_inicio;

	
	protected $factura_final;

	
	protected $prefactura;

	
	protected $aKernelUbicacion;

	
	protected $alreadyInSave = false;

	
	protected $alreadyInValidation = false;

	
	public function __construct()
	{
		$this->applyDefaultValues();
	}

	
	public function applyDefaultValues()
	{
	}

	
	public function getId()
	{
		return $this->id;
	}

	
	public function getNombre()
	{
		return $this->nombre;
	}

	
	public function getResolucion()
	{
		return $this->resolucion;
	}

	
	public function getUbicacionid()
	{
		return $this->ubicacionid;
	}

	
	public function getFacturaInicio()
	{
		return $this->factura_inicio;
	}

	
	public function getFacturaFinal()
	{
		return $this->factura_final;
	}

	
	public function getPrefactura()
	{
		return $this->prefactura;
	}

	
	public function setId($v)
	{
		if ($v !== null) {
			$v = (string) $v;
		}

		if ($this->id !== $v) {
			$this->id = $v;
			$this->modifiedColumns[] = KernelSucursalPeer::ID;
		}

		return $this;
	} 
	
	public function setNombre($v)
	{
		if ($v !== null) {
			$v = (string) $v;
		}

		if ($this->nombre !== $v) {
			$this->nombre = $v;
			$this->modifiedColumns[] = KernelSucursalPeer::NOMBRE;
		}

		return $this;
	} 
	
	public function setResolucion($v)
	{
		if ($v !== null) {
			$v = (string) $v;
		}

		if ($this->resolucion !== $v) {
			$this->resolucion = $v;
			$this->modifiedColumns[] = KernelSucursalPeer::RESOLUCION;
		}

		return $this;
	} 
	
	public function setUbicacionid($v)
	{
		if ($v !== null) {
			$v = (int) $v;
		}

		if ($this->ubicacionid !== $v) {
			$this->ubicacionid = $v;
			$this->modifiedColumns[] = KernelSucursalPeer::UBICACIONID;
		}

		if ($this->aKernelUbicacion !== null && $this->aKernelUbicacion->getId() !== $v) {
			$this->aKernelUbicacion = null;
		}

		return $this;
	} 
	
	public function setFacturaInicio($v)
	{
		if ($v !== null) {
			$v = (string) $v;
		}

		if ($this->factura_inicio !== $v) {
			$this->factura_inicio = $v;
			$this->modifiedColumns[] = KernelSucursalPeer::FACTURA_INICIO;
		}

		return $this;
	} 
	
	public function setFacturaFinal($v)
	{
		if ($v !== null) {
			$v = (string) $v;
		}

		if ($this->factura_final !== $v) {
			$this->factura_final = $v;
			$this->modifiedColumns[] = KernelSucursalPeer::FACTURA_FINAL;
		}

		return $this;
	} 
	
	public function setPrefactura($v)
	{
		if ($v !== null) {
			$v = (string) $v;
		}

		if ($this->prefactura !== $v) {
			$this->prefactura = $v;
			$this->modifiedColumns[] = KernelSucursalPeer::PREFACTURA;
		}

		return $this;
	} 
	
	public function hasOnlyDefaultValues()
	{
						if (array_diff($this->modifiedColumns, array())) {
				return false;
			}

				return true;
	} 
	
	public function hydrate($row, $startcol = 0, $rehydrate = false)
	{
		try {

			$this->id = ($row[$startcol + 0] !== null) ? (string) $row[$startcol + 0] : null;
			$this->nombre = ($row[$startcol + 1] !== null) ? (string) $row[$startcol + 1] : null;
			$this->resolucion = ($row[$startcol + 2] !== null) ? (string) $row[$startcol + 2] : null;
			$this->ubicacionid = ($row[$startcol + 3] !== null) ? (int) $row[$startcol + 3] : null;
			$this->factura_inicio = ($row[$startcol + 4] !== null) ? (string) $row[$startcol + 4] : null;
			$this->factura_final = ($row[$startcol + 5] !== null) ? (string) $row[$startcol + 5] : null;
			$this->prefactura = ($row[$startcol + 6] !== null) ? (string) $row[$startcol + 6] : null;
			$this->resetModified();

			$this->setNew(false);

			if ($rehydrate) {
				$this->ensureConsistency();
			}

						return $startcol + 7; 
		} catch (Exception $e) {
			throw new PropelException("Error populating KernelSucursal object", $e);
		}
	}

	
	public function ensureConsistency()
	{

		if ($this->aKernelUbicacion !== null && $this->ubicacionid !== $this->aKernelUbicacion->getId()) {
			$this->aKernelUbicacion = null;
		}
	
	} 
	
	public function reload($deep = false, PropelPDO $con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("Cannot reload a deleted object.");
		}

		if ($this->isNew()) {
			throw new PropelException("Cannot reload an unsaved object.");
		}

		if ($con === null) {
			$con = Propel::getConnection(KernelSucursalPeer::DATABASE_NAME, Propel::CONNECTION_READ);
		}

				
		$stmt = KernelSucursalPeer::doSelectStmt($this->buildPkeyCriteria(), $con);
		$row = $stmt->fetch(PDO::FETCH_NUM);
		$stmt->closeCursor();
		if (!$row) {
			throw new PropelException('Cannot find matching row in the database to reload object values.');
		}
		$this->hydrate($row, 0, true); 
		if ($deep) {  
			$this->aKernelUbicacion = null;
		} 	}

	
	public function delete(PropelPDO $con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("This object has already been deleted.");
		}

		if ($con === null) {
			$con = Propel::getConnection(KernelSucursalPeer::DATABASE_NAME, Propel::CONNECTION_WRITE);
		}

		try {
			$con->beginTransaction();
			KernelSucursalPeer::doDelete($this, $con);
			$this->setDeleted(true);
			$con->commit();
		} catch (PropelException $e) {
			$con->rollback();
			throw $e;
		}
	}

	
	public function save(PropelPDO $con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("You cannot save an object that has been deleted.");
		}

		if ($con === null) {
			$con = Propel::getConnection(KernelSucursalPeer::DATABASE_NAME, Propel::CONNECTION_WRITE);
		}

		try {
		    if($this->validate())
		    {
    			$con->beginTransaction();
    			$affectedRows = $this->doSave($con);
    			$con->commit();
    			KernelSucursalPeer::addInstanceToPool($this);
    			return true;
			}else
			{
			     
                 return false;
            }
		} catch (PropelException $e) {
			$con->rollback();
			
			
			ErrorLog::reportarError('errors', 'error',  $e->getMessage() );
			return false;
		}
	}

	
	protected function doSave(PropelPDO $con)
	{
		$affectedRows = 0; 		if (!$this->alreadyInSave) {
			$this->alreadyInSave = true;

												
			if ($this->aKernelUbicacion !== null) {
				if ($this->aKernelUbicacion->isModified() || $this->aKernelUbicacion->isNew()) {
					$affectedRows += $this->aKernelUbicacion->save($con);
				}
				$this->setKernelUbicacion($this->aKernelUbicacion);
			}


						if ($this->isModified()) {
				if ($this->isNew()) {
					$pk = KernelSucursalPeer::doInsert($this, $con);
					$affectedRows += 1; 										 										 
					$this->setId($pk);  
					$this->setNew(false);
				} else {
					$affectedRows += KernelSucursalPeer::doUpdate($this, $con);
				}

				$this->resetModified(); 			}

			$this->alreadyInSave = false;

		}
		return $affectedRows;
	} 
	
	protected $validationFailures = array();

	
	public function getValidationFailures()
	{
		return $this->validationFailures;
	}

	
	public function validate($columns = null)
	{
		$res = $this->doValidate($columns);
		if ($res === true) {
			$this->validationFailures = array();
			return true;
		} else {
			$this->validationFailures = $res;
			return false;
		}
	}

	
	protected function doValidate($columns = null)
	{
		if (!$this->alreadyInValidation) {
			$this->alreadyInValidation = true;
			$retval = null;

			$failureMap = array();


												
			if ($this->aKernelUbicacion !== null) {
				if (!$this->aKernelUbicacion->validate($columns)) {
					$failureMap = array_merge($failureMap, $this->aKernelUbicacion->getValidationFailures());
				}
			}


			if (($retval = KernelSucursalPeer::doValidate($this, $columns)) !== true) {
				$failureMap = array_merge($failureMap, $retval);
			}



			$this->alreadyInValidation = false;
		}

		return (!empty($failureMap) ? $failureMap : true);
	}

	
	public function getByName($name, $type = BasePeer::TYPE_PHPNAME)
	{
		$pos = KernelSucursalPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		return $this->getByPosition($pos);
	}

	
	public function getByPosition($pos)
	{
		switch($pos) {
			case 0:
				return $this->getId();
				break;
			case 1:
				return $this->getNombre();
				break;
			case 2:
				return $this->getResolucion();
				break;
			case 3:
				return $this->getUbicacionid();
				break;
			case 4:
				return $this->getFacturaInicio();
				break;
			case 5:
				return $this->getFacturaFinal();
				break;
			case 6:
				return $this->getPrefactura();
				break;
			default:
				return null;
				break;
		} 	}

	
	public function toArray($keyType = BasePeer::TYPE_PHPNAME, $includeLazyLoadColumns = true)
	{
		$keys = KernelSucursalPeer::getFieldNames($keyType);
		$result = array(
			$keys[0] => $this->getId(),
			$keys[1] => $this->getNombre(),
			$keys[2] => $this->getResolucion(),
			$keys[3] => $this->getUbicacionid(),
			$keys[4] => $this->getFacturaInicio(),
			$keys[5] => $this->getFacturaFinal(),
			$keys[6] => $this->getPrefactura(),
		);
		return $result;
	}

	
	public function setByName($name, $value, $type = BasePeer::TYPE_PHPNAME)
	{
		$pos = KernelSucursalPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		return $this->setByPosition($pos, $value);
	}

	
	public function setByPosition($pos, $value)
	{
		switch($pos) {
			case 0:
				$this->setId($value);
				break;
			case 1:
				$this->setNombre($value);
				break;
			case 2:
				$this->setResolucion($value);
				break;
			case 3:
				$this->setUbicacionid($value);
				break;
			case 4:
				$this->setFacturaInicio($value);
				break;
			case 5:
				$this->setFacturaFinal($value);
				break;
			case 6:
				$this->setPrefactura($value);
				break;
		} 	}

	
	public function fromArray($arr, $keyType = BasePeer::TYPE_PHPNAME)
	{
		$keys = KernelSucursalPeer::getFieldNames($keyType);

		if (array_key_exists($keys[0], $arr)) $this->setId($arr[$keys[0]]);
		if (array_key_exists($keys[1], $arr)) $this->setNombre($arr[$keys[1]]);
		if (array_key_exists($keys[2], $arr)) $this->setResolucion($arr[$keys[2]]);
		if (array_key_exists($keys[3], $arr)) $this->setUbicacionid($arr[$keys[3]]);
		if (array_key_exists($keys[4], $arr)) $this->setFacturaInicio($arr[$keys[4]]);
		if (array_key_exists($keys[5], $arr)) $this->setFacturaFinal($arr[$keys[5]]);
		if (array_key_exists($keys[6], $arr)) $this->setPrefactura($arr[$keys[6]]);
	}

	
	public function buildCriteria()
	{
		$criteria = new Criteria(KernelSucursalPeer::DATABASE_NAME);

		if ($this->isColumnModified(KernelSucursalPeer::ID)) $criteria->add(KernelSucursalPeer::ID, $this->id);
		if ($this->isColumnModified(KernelSucursalPeer::NOMBRE)) $criteria->add(KernelSucursalPeer::NOMBRE, $this->nombre);
		if ($this->isColumnModified(KernelSucursalPeer::RESOLUCION)) $criteria->add(KernelSucursalPeer::RESOLUCION, $this->resolucion);
		if ($this->isColumnModified(KernelSucursalPeer::UBICACIONID)) $criteria->add(KernelSucursalPeer::UBICACIONID, $this->ubicacionid);
		if ($this->isColumnModified(KernelSucursalPeer::FACTURA_INICIO)) $criteria->add(KernelSucursalPeer::FACTURA_INICIO, $this->factura_inicio);
		if ($this->isColumnModified(KernelSucursalPeer::FACTURA_FINAL)) $criteria->add(KernelSucursalPeer::FACTURA_FINAL, $this->factura_final);
		if ($this->isColumnModified(KernelSucursalPeer::PREFACTURA)) $criteria->add(KernelSucursalPeer::PREFACTURA, $this->prefactura);

		return $criteria;
	}

	
	public function buildPkeyCriteria()
	{
		$criteria = new Criteria(KernelSucursalPeer::DATABASE_NAME);

		$criteria->add(KernelSucursalPeer::ID, $this->id);

		return $criteria;
	}

	
	public function getPrimaryKey()
	{
		return $this->getId();
	}

	
	public function setPrimaryKey($key)
	{
		$this->setId($key);
	}

	
	public function copyInto($copyObj, $deepCopy = false)
	{

		$copyObj->setNombre($this->nombre);

		$copyObj->setResolucion($this->resolucion);

		$copyObj->setUbicacionid($this->ubicacionid);

		$copyObj->setFacturaInicio($this->factura_inicio);

		$copyObj->setFacturaFinal($this->factura_final);

		$copyObj->setPrefactura($this->prefactura);


		$copyObj->setNew(true);

		$copyObj->setId(NULL); 
	}

	
	public function copy($deepCopy = false)
	{
				$clazz = get_class($this);
		$copyObj = new $clazz();
		$this->copyInto($copyObj, $deepCopy);
		return $copyObj;
	}

	
	public function getPeer()
	{
		if (self::$peer === null) {
			self::$peer = new KernelSucursalPeer();
		}
		return self::$peer;
	}

	
	public function setKernelUbicacion(KernelUbicacion $v = null)
	{
		if ($v === null) {
			$this->setUbicacionid(NULL);
		} else {
			$this->setUbicacionid($v->getId());
		}

		$this->aKernelUbicacion = $v;

						if ($v !== null) {
			$v->addKernelSucursal($this);
		}

		return $this;
	}


	
	public function getKernelUbicacion(PropelPDO $con = null)
	{
		if ($this->aKernelUbicacion === null && ($this->ubicacionid !== null)) {
			$this->aKernelUbicacion = KernelUbicacionPeer::retrieveByPK($this->ubicacionid, $con);
			
		}
		return $this->aKernelUbicacion;
	}

	
	public function clearAllReferences($deep = false)
	{
		if ($deep) {
		} 
	}

} 